/*****************************************************************************************************************************************
******Code for Currie, Mueller-Smith, Rossin-Slater "Violence while in Utero: The Impacts of Assaults During Pregnancy on Birth Outcomes**
******July 2020***********************************************************************************************************************/


**** THIS DO-FILE MERGES THE BIRTH OUTCOMES DATA TO THE MATERNAL ADDRESS CHARACTERISTICS AND CRIME DATA ************


set more off
set matsize 10000

clear all

cap log close

***** Paths to data set locations, results graphs *****
global bulk 
global home 
global results 
global graphs

**** First, run clean_birthoutcomes.do file to clean the births data

****CLEAN CRIME DATA ***** 


use ${bulk}direct_crime.dta, clear
*many records have multiple crimes associated with them --> these are big apartment buildings

*one obs is missing Crime_RMC--> drop it

drop if Crime_RMC==.


*create dummy variables for classes of crimes
gen felony = Lawclass=="F"
gen misdemeanor = Lawclass=="M"
gen violation = Lawclass=="V"

*create dummy variables for offense codes (group some of them)

gen arson = OffenseCd==2

gen assault = OffenseCd==3 | OffenseCd==9
*includes felony and non-felony assaults

gen f_assault = OffenseCd==9
*felony assaults only

gen burglary = OffenseCd==4|OffenseCd==20
*includes burglary and robbery


gen drugs = OffenseCd==7
gen kidnap = OffenseCd==8

gen fraud = OffenseCd==10
gen larceny = OffenseCd==11|OffenseCd==12|OffenseCd==19
*inclues vehicle and non-vehicle larceny, and petit larceny

gen harassment = OffenseCd==13
gen homicide = OffenseCd==14

gen dui = OffenseCd==15

gen weapons = OffenseCd==22

gen other = OffenseCd==18|OffenseCd==1|OffenseCd==16|OffenseCd==17|OffenseCd==21|OffenseCd==5|OffenseCd==6
*includes admin codes, miscelaneous penal law, offenses against public administration, vehicle and traffic, criminal mischief, trespassing and other


global crimeclass felony misdemeanor violation

global crimetypes arson assault f_assault burglary drugs kidnap fraud larceny harassment homicide dui weapons other


**** reshape data to calculate number and types of crimes by month relative to conception

sort Study_ID Crime_RMC
by Study_ID Crime_RMC: gen totcrimes = _N

foreach class in $crimeclass {
sort Study_ID Crime_RMC
by Study_ID Crime_RMC: egen tot`class' = total(`class')
}

foreach type in $crimetypes {
sort Study_ID Crime_RMC
by Study_ID Crime_RMC: egen tot`type' = total(`type')
}

keep Study_ID Crime_RMC tot*

bys Study_ID Crime_RMC: keep if _n==1

*****Crime_RMC takes on values -15 to 45 -- need to transform so that it's all positive
*add 15:
replace Crime_RMC = Crime_RMC+15
***note: now, values 0-14 are months before conception; 15-24 are during 9 months of pregnancy; 25-60 are after end of pregnancy

*reshape data set so we have one obs per Study_ID

reshape wide tot*, i(Study_ID) j(Crime_RMC)

sum totcrimes*

********rename so that timing is more clear

*this is for months before conception:

ren totcrimes14 totcrimes_pre1
ren totcrimes13 totcrimes_pre2
ren totcrimes12 totcrimes_pre3
ren totcrimes11 totcrimes_pre4
ren totcrimes10 totcrimes_pre5
ren totcrimes9 totcrimes_pre6
ren totcrimes8 totcrimes_pre7
ren totcrimes7 totcrimes_pre8
ren totcrimes6 totcrimes_pre9
ren totcrimes5 totcrimes_pre10
ren totcrimes4 totcrimes_pre11
ren totcrimes3 totcrimes_pre12

drop totcrimes2 totcrimes1 totcrimes0
*these more than 1 year before conception

foreach class in $crimeclass {
ren tot`class'14 tot`class'_pre1
ren tot`class'13 tot`class'_pre2
ren tot`class'12 tot`class'_pre3
ren tot`class'11 tot`class'_pre4
ren tot`class'10 tot`class'_pre5
ren tot`class'9 tot`class'_pre6
ren tot`class'8 tot`class'_pre7
ren tot`class'7 tot`class'_pre8
ren tot`class'6 tot`class'_pre9
ren tot`class'5 tot`class'_pre10
ren tot`class'4 tot`class'_pre11
ren tot`class'3 tot`class'_pre12

drop tot`class'2 tot`class'1 tot`class'0
}


foreach type in $crimetypes {
ren tot`type'14 tot`type'_pre1
ren tot`type'13 tot`type'_pre2
ren tot`type'12 tot`type'_pre3
ren tot`type'11 tot`type'_pre4
ren tot`type'10 tot`type'_pre5
ren tot`type'9 tot`type'_pre6
ren tot`type'8 tot`type'_pre7
ren tot`type'7 tot`type'_pre8
ren tot`type'6 tot`type'_pre9
ren tot`type'5 tot`type'_pre10
ren tot`type'4 tot`type'_pre11
ren tot`type'3 tot`type'_pre12

drop tot`type'2 tot`type'1 tot`type'0
}



*this is for months after conception:
forval i=15/60 {
local j=`i'-15
ren totcrimes`i' totcrimes_post`j'
foreach class in $crimeclass {
ren tot`class'`i' tot`class'_post`j'
}
foreach type in $crimetypes {
ren tot`type'`i' tot`type'_post`j'
}
}

sum totcrimes*

*** change missings to zeros (i.e. no crimes in that month)

foreach var of varlist tot* {
replace `var' = 0 if `var'==.
}

forval i=1/12 {
label var totcrimes_pre`i' "Tot. Crimes in Month `i' Before Concep"
foreach class in $crimeclass {
label var tot`class'_pre`i' "Tot. Crimes (`class') in Month `i' Before Concep"
}
foreach type in $crimetypes {
label var tot`type'_pre`i' "Tot. Crimes (`type') in Month `i' Before Concep"
}
}

forval i=0/45 {
label var totcrimes_post`i' "Tot. Crimes in Month `i' After Concep"
foreach class in $crimeclass {
label var tot`class'_post`i' "Tot. Crimes (`class') in Month `i' After Concep"
}
foreach type in $crimetypes {
label var tot`type'_post`i' "Tot. Crimes (`type') in Month `i' After Concep"
}
}

save ${bulk}direct_crimes_UNIQUEID.dta, replace

*****************************************************************

****** Now, merge to the clean births data *********

******* input clean births data:

use ${bulk}birth_outcomes_CLEAN.dta, clear

*verify Study_ID variable
isid Study_ID

tab birth_year

*create dummy variable for mom residence being outside NYC

gen mom_outside_NYC = mom_borough==6

*Drop moms from outside NYC
drop if mom_outside_NYC==1


**********merge to maternal address characteristics data

merge 1:1 Study_ID using ${bulk}maternal_address_chars.dta, keep(match master)

tab _merge

tab birth_year if _merge==1

tab mom_borough if _merge==1

ren _merge merge_address

* rename/ label address characteristics variables

ren BldgClass bldgclass
ren NumBldgs numbldgs
ren NumFloors numfloors
ren UnitsRes unitsres
ren AssessTot assesstot
ren YearBuilt year_built
ren Tract2000 tract2000

label var bldgclass "Building Class"
label var numbldgs "Number Buildings in Complex/Project"
label var numfloors "Number Floors in Building"
label var unitsres "Number Residential Units in Building"
label var assesstot "Assessed Value of Building"
label var year_built "Year Building Was Built"
label var tract2000 "2000 Census Tract"

gen apartment=bldgclass=="D"|bldgclass=="B"|bldgclass=="C"|bldgclass=="R"
*includes apartment bldgs, condominiums, and two+ family homes

gen house=bldgclass=="A"
*only single-family homes


label var apartment "Apartment Bldg"

********************************************************************************
********************************************************************************
********************************************************************************

*************** merge to crime data

merge 1:1 Study_ID using ${bulk}direct_crimes_UNIQUEID.dta, keep(match master)

tab _merge

*replace unmerged observations as zeros (i.e. no crimes)
foreach var of varlist tot* {
replace `var' = 0 if _merge==1
}

drop _merge


global crimeclass felony misdemeanor violation

global crimetypes arson assault f_assault burglary drugs kidnap fraud larceny harassment homicide dui weapons other




**** calculate the total number of crimes during pregnancy

egen totcrimes_preg = rowtotal(totcrimes_post0 totcrimes_post1 totcrimes_post2 totcrimes_post3 totcrimes_post4 totcrimes_post5 totcrimes_post6 totcrimes_post7 totcrimes_post8 totcrimes_post9)

foreach class in $crimeclass {
egen tot`class'_preg = rowtotal(tot`class'_post0 tot`class'_post1 tot`class'_post2 tot`class'_post3 tot`class'_post4 tot`class'_post5 tot`class'_post6 tot`class'_post7 tot`class'_post8 tot`class'_post9)
}

foreach type in $crimetypes {
egen tot`type'_preg = rowtotal(tot`type'_post0 tot`type'_post1 tot`type'_post2 tot`type'_post3 tot`type'_post4 tot`type'_post5 tot`type'_post6 tot`type'_post7 tot`type'_post8 tot`type'_post9)
}



******************************************************
******************************************************
****save merged data

save ${bulk}births_crimes.dta, replace

